#include "Sprite.h"
#include "DXGraphics.h"
#include "Global.h"

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{

	// Enable run-time memory check for debug builds.
	#if defined(DEBUG) | defined(_DEBUG)
		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif

	DXGraphics graphics ;
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          L"WindowClass",
						  L"GSP281 Simulation #1 by David Fairweather",
                          WS_OVERLAPPEDWINDOW,
                          0, 0,
                          SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

	if( !hWnd )
	{
		MessageBoxA(0, "CreateWindow FAILED", 0, 0);
		PostQuitMessage(0);
	}

	DirectInput di(DISCL_NONEXCLUSIVE |
		DISCL_FOREGROUND,
		DISCL_NONEXCLUSIVE |
		DISCL_FOREGROUND, hInstance, hWnd ) ;

	gDInput = &di ;

    ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

    // set up and initialize Direct3D
    graphics.initD3D(hWnd);

    // enter the main loop:

    MSG msg;

	_int64 cntsPerSec = 0 ;
	QueryPerformanceFrequency( (LARGE_INTEGER*)&cntsPerSec ) ;
	float secsPerCnt = 1.0f / (float)cntsPerSec ;

	_int64 prevTimeStamp = 0 ;
	QueryPerformanceCounter( (LARGE_INTEGER*)&prevTimeStamp ) ;

    while(TRUE)
    {
        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
		
		_int64 currTimeStamp = 0 ;
		QueryPerformanceCounter( (LARGE_INTEGER*)&currTimeStamp ) ;
		float dt = ( currTimeStamp - prevTimeStamp ) * secsPerCnt ;

		graphics.Update( dt ) ;

        graphics.render_frame( ) ;

		prevTimeStamp = currTimeStamp ;

        while ((GetTickCount() - starting_point) < 25);
    }

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}